package cli.menu;

/**
 * BudgetBuoy
 * 
 * Simplify your personal expense management
 * 
 * @team cs2103jan13-w15-3j
 */

//--------------------------------------------------------------

/**
 * DeleteCategory Class
 * 
 * Description: 
 * 
 * 
 * @package   cli.menu
 * @author    Lai Yong Quan (A0080909Y)
 * @edited by: Tan Si Ling (A0085314J)
 */

import java.util.ArrayList;
import shared.*;
import cli.tools.Process;



class DeleteCategory extends Function{

	private static final String[] DELETECAT = {
		"-= Main Menu > Manage Categories > Delete Category =-",
		"[b]: back [u]: undo [r]: redo [h]: home menu",
		"",
		"Select the Category to delete by keying in its index:"	
	};
	
	private static final String[] DESTCAT = {
		"-= Main Menu > Manage Categories > Delete Category =-",
		"[b]: back [u]: undo [r]: redo [h]: home menu",
		"",
		"Select the destination Category by keying in its index:"	
	};

	private static final String[] SELECT = {
		"-= Main Menu > Manage Categories > Delete Category =-",
		"[b]: back [u]: undo [r]: redo [h]: home menu",
		"",
		"Would you like to select a Category to shift the items to? Y/N",
		"If you select no (N), they will automatically be placed into the 'Uncategorised' Category.",
	};

	private static State BACK = State.MANAGECATEGORIES;
	private static State CURRENT = State.DELETECATEGORY;

	public static State runMenu(){

	//	cli.print(DELETECAT);

		Category toDelete;
		// set default destination to Uncategorised (index 0)

		Object choice = tools.selectFromList(cli.getApi().getCategoryList(),"manageCategories",DELETECAT);

		//if back signal sent
		if (choice == null) {
			return BACK;
		}

		// if received Instruction object (divert to another menu)
		if (choice.getClass() == State.class) {
			return (State) choice;
		}

		// last case : if choice was really the correct object
		toDelete = (Category)choice;

		if (toDelete.equals(cli.getApi().getCategoryList().get(0))) {
			cli.println("Unable to delete 'Uncategorised'");
			cli.println("Returning to previous menu");

			return BACK;
		}

		String input = null;
		
		while(input == null || input.length() == 0){
			cli.clearScreen();
		cli.print(SELECT);
		input = cli.nextLine();
		}

	

		return processInput(input, toDelete);

	}


	private static State processInput(String input, Category toDelete){

		char charInput = input.charAt(0);

		Category destination = cli.getApi().getCategoryList().get(0);

		switch (charInput) {

		case 'Y':
		case 'y':
			//cli.println("Select the destination Category by keying in its index:");
			Object choice = tools.selectFromList(cli.getApi().getCategoryList(), "manageCategories", DESTCAT);

			//if back signal sent
			if (choice == null) {
				return State.MANAGECATEGORIES;
			}

			// if received Instruction object (divert to another menu)
			if (choice.getClass() == State.class) {
				return (State) choice;
			}

			// last case : if choice was really the correct object
			destination = (Category)choice;
			break;

		default:
			break;

		}

		try{
			ArrayList<Record> recordList = cli.getApi().mergeCategory(toDelete, destination);

			// Si Ling: **Keeping Track of the Actions DONE**
			tools.action.addToUndoStack(toDelete, destination,
					Process.MERGECATEGORIES,  recordList);
			// **END**
		}
		catch(Exception e){
			cli.println("An error has occurred - aborting");
		}

		return CURRENT;

	}
}